﻿using System;
using System.Collections.Generic;
using System.Linq;
using IterationLife.ContentResources.Enums;
using IterationLife.ContentResources.Models;
using IterationLife.Extensions.Dtos;
using IterationLife.Extensions.Helpers;
using Util;

namespace IterationLife.Mobile.AppServices.Dtos.Common
{
    /// <summary>
    /// 内容资源详情[输出]
    /// </summary>
    public class ContentResourceDetailOutPut
    {
        /// <summary>
        /// 初始化内容资源详情[输出]
        /// </summary>
        public ContentResourceDetailOutPut()
        {
            Pictures = new List<ContentResourcePictureOutPut>();
            Video = new Resources();
            Tags = new List<string>();
        }

        /// <summary>
        /// 编号
        /// </summary>
        public Guid Id { get; set; }

        /// <summary>
        /// 类型
        /// </summary>
        public int? Type { get; set; }

        /// <summary>
        /// 标题
        /// </summary>
        public string Title { get; set; }

        /// <summary>
        /// 标签
        /// </summary>
        public List<string> Tags { get; set; }

        /// <summary>
        /// 时间
        /// </summary>
        public string Time { get; set; }

        /// <summary>
        /// 富文本
        /// </summary>
        public string RichText { get; set; }

        /// <summary>
        /// 视频封面图
        /// </summary>
        public string VideoBanner { get; set; }

        /// <summary>
        /// 视频
        /// </summary>
        public Resources Video { get; set; }

        /// <summary>
        /// 简介
        /// </summary>
        public string BriefIntroduction { get; set; }

        /// <summary>
        /// 内容资源图片集合
        /// </summary>
        public List<ContentResourcePictureOutPut> Pictures { get; set; }
    }

    /// <summary>
    /// 内容资源详情[输出]扩展
    /// </summary>
    public static class ContentResourceDetailOutPutExtension
    {
        /// <summary>
        /// 转换为内容资源列表[输出]
        /// </summary>
        /// <param name="entity">内容资源</param>
        public static ContentResourceDetailOutPut ToNewsDetailOutPut(this ContentResource entity)
        {
            var result = new ContentResourceDetailOutPut
            {
                Id = entity.Id,
                Type = entity.Type.Value(),
                Title = entity.Title,
                Tags = Util.Helpers.Convert.ToList<string>(entity.Tags),
                Time = TimeHelper.IntervalDescription(entity.PublicationTime.SafeValue(), DateTime.Now),
                RichText = entity.RichText,
                Video = entity.Video,
                BriefIntroduction = entity.BriefIntroduction,
                Pictures = entity.ContentResourcePictures.Select(t => t.ToNewsPictureOutPut()).OrderBy(t => t.SortId).ToList()
            };
            if (entity.Type != ContentResourceType.Video)
                return result;
            result.VideoBanner = entity.BannerType == ContentResourceBannerType.Single
                                 ? entity.SingleBanner.Address
                                 : entity.MultiBanners[0].Address;
            return result;
        }
    }
}
